package com.xiyue.server.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.sport.common.utils.PasswordUtils;
import com.sport.common.web.controller.BaseController;
import com.sport.common.web.model.PageParams;
import com.sport.common.web.model.Result;
import com.sport.model.dto.Option;
import com.sport.model.entity.SysUser;
import com.xiyue.server.annotation.RestControllerMapping;
import com.xiyue.server.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestControllerMapping("sysUser")
@Tag(name = "系统用户管理")
@ApiSupport(author = "xiyue:13505961579",order = 1)
public class SysUserController extends BaseController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @GetMapping("info/{id}")
    @Operation(summary = "通过id查询单条数据",parameters = {
            @Parameter(name = "id",description = "所查询的主键id"
                    ,required = true,in = ParameterIn.PATH)
    })
    @ApiOperationSupport(order = 3)
    public Result<SysUser> queryById(@PathVariable String id){
        return Result.success(sysUserService.getById(id));
    }


    /**
     * 分页查询
     *
     * @return 查询结果
     */

    @PostMapping("/page")
    @Operation(summary = "分页查询")
    @ApiOperationSupport(order = 5)
    public Result<Page<SysUser>> pageQuery(@RequestBody PageParams<SysUser> pageParams){
        SysUser params = pageParams.getParams();
        LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<>();
        query.like(StrUtil.isNotBlank(params.getNickname())&&StrUtil.isNotEmpty(params.getNickname()), SysUser::getNickname, params.getNickname());
        Page<SysUser> page = sysUserService.page(pageParams.getPage(), query);
        page.getRecords().forEach(sysUser -> {
            sysUser.setRoles(sysUserService.getRoles(sysUser.getId()));
        });
        return Result.success(page);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */

    @PostMapping("/list")
    @Operation(summary = "列表查询")
    @ApiOperationSupport(order = 2)
    public Result<List<SysUser>> pageQuery(@RequestBody SysUser user){
        LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<>();
        query.like(StrUtil.isEmpty(user.getNickname())&&StrUtil.isNotBlank(user.getNickname()), SysUser::getNickname, user.getNickname());
        query.eq(user.getSex() != null,SysUser::getSex,user.getSex());
        return Result.success(sysUserService.list(query));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */

    @PostMapping("add")
    @Operation(summary = "新增数据")
    @ApiOperationSupport(order = 1)
    public Result<SysUser> add(@RequestBody SysUser sysUser){
        int rows = sysUserService.addSysUser(sysUser);
        if(rows >0){
            List<Option> roles = sysUser.getRoles();
            if(roles.size()>0){
                List<Object> roleIds = roles.stream().map(Option::getValue).toList();
                sysUserService.insertRoles(sysUser.getId(), roleIds);
            }
        }
        return toResult(rows);
    }

    /**
     * 更新数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */

    @PostMapping("update")
    @Operation(summary = "更新系统用户数据")
    @ApiOperationSupport(order = 4)
    public Result<SysUser> edit(@RequestBody SysUser sysUser){
        sysUserService.deleteRole(sysUser.getId());
        List<Option> roles = sysUser.getRoles();
        if(roles.size()>0){
            List<Object> roleIds = roles.stream().map(Option::getValue).toList();
            sysUserService.insertRoles(sysUser.getId(), roleIds);
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */

    @GetMapping("/delete/{ids}")
    @Operation(summary = "通过主键删除数据",parameters = {
            @Parameter(name = "id",description = "所删除数据的主键id"
                    ,required = true,in = ParameterIn.PATH)
    })
    @ApiOperationSupport(order = 2)
    public Result<SysUser> deleteById(@PathVariable String ids){
        return toResult(sysUserService.removeById(ids));
    }
}
